home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet internetowy / Przegladarki internetowe / Mozilla Seamonkey 1.0.5 pl / seamonkey-1.0.5.pl-PL.win32.installer.exe / BROWSER.XPI / bin / chrome / toolkit.jar / content / global / commonDialog.js < prev    next >
Encoding:
JavaScript  |  2006-03-20  |  11.1 KB  |  343 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Mozilla Communicator client code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 1998
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Alec Flett  <alecf@netscape.com>
  23.  *   Ben Goodger <ben@netscape.com>
  24.  *   Blake Ross  <blakeross@telocity.com>
  25.  *   Joe Hewitt <hewitt@netscape.com>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  29.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the MPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the MPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. // parameters to gCommonDialogParam.Get() are defined in nsPIPromptService.idl
  42. var gCommonDialogParam = 
  43.   window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  44.   
  45. function showControls()
  46. {
  47.   // This is called before onload fires, so we can't be certain that any elements
  48.   // in the document have their bindings ready, so don't call any methods/properties
  49.   // here on xul elements that come from xbl bindings.
  50.   
  51.   // show the required textboxes and set their initial values
  52.   var nTextBoxes = gCommonDialogParam.GetInt(3);
  53.   if (nTextBoxes == 2) {
  54.     if (gCommonDialogParam.GetInt(4) == 1) {
  55.       initTextbox("password1", 4, 6, false);
  56.       initTextbox("password2", 5, 7, false);
  57.     }
  58.     else {
  59.       initTextbox("login", 4, 6, false);
  60.       initTextbox("password1", 5, 7, false);
  61.     }
  62.   } else if (nTextBoxes == 1) {
  63.     if (gCommonDialogParam.GetInt(4) == 1)
  64.       initTextbox("password1", -1, 6, true);
  65.     else
  66.       initTextbox("login", 4, 6, true);
  67.   }
  68. }
  69.  
  70. function setLabelForNode(aNode, aLabel, aIsLabelFlag)
  71. {
  72.   // This is for labels with possible accesskeys
  73.   var accessKeyIndex;
  74.   if (/^\&[^\&]/.test(aLabel)) { // access key is at the start
  75.    accessKeyIndex = 0;
  76.   } else {
  77.     accessKeyIndex = aLabel.search(/[^\&]\&[^\&]/) + 1;
  78.     if (accessKeyIndex == 0) {
  79.       accessKeyIndex = -1; // magic value for no accesskey
  80.     }
  81.   }
  82.  
  83.   // If a character has an & before it, then it should become an accesskey
  84.   if (accessKeyIndex >= 0 && accessKeyIndex < aLabel.length - 1) {
  85.     // This will also cause the accesskey attribute to be set via xbl
  86.     aNode.accessKey = aLabel.charAt(accessKeyIndex + 1);
  87.     // Set the label to the string without the &
  88.     aLabel = aLabel.substr(0, accessKeyIndex) + 
  89.              aLabel.substr(accessKeyIndex + 1);
  90.   }
  91.   aLabel = aLabel.replace(/\&\&/g, "&");
  92.   if (aIsLabelFlag) {    // Set text for <label> element
  93.     aNode.setAttribute("value", aLabel);
  94.   } else {    // Set text for other xul elements
  95.     aNode.label = aLabel;
  96.   }
  97. }
  98.  
  99. function commonDialogOnLoad()
  100. {
  101.   // Set the document title. On Mac, we only care about URI's.
  102.   if (/Mac/.test(navigator.platform)) {
  103.     var titleString = gCommonDialogParam.GetString(12);
  104.     if (/^\w+:\/\/\w/.test(titleString)) {
  105.       setElementText("info.title", titleString, true);
  106.       unHideElementById("info.title"); // Show URI inside dialog.
  107.     }
  108.   }
  109.   else {
  110.     document.title = gCommonDialogParam.GetString(12);
  111.   }
  112.  
  113.   // set the number of command buttons
  114.   var nButtons = gCommonDialogParam.GetInt(2);
  115.   var dialog = document.documentElement;
  116.   switch (nButtons) {
  117.     case 1:
  118.       dialog.getButton("cancel").hidden = true;
  119.       break;
  120.     case 4:
  121.       dialog.getButton("extra2").hidden = false;
  122.     case 3:
  123.       dialog.getButton("extra1").hidden = false;
  124.   }
  125.  
  126.   // display the main text
  127.   var messageText = gCommonDialogParam.GetString(0);
  128.   var messageParent = document.getElementById("info.box");
  129.   var messageParagraphs = messageText.split("\n");
  130.  
  131.   for (var i = 0; i < messageParagraphs.length; i++) {
  132.     var descriptionNode = document.createElement("description");
  133.     var text = document.createTextNode(messageParagraphs[i]);
  134.     descriptionNode.appendChild(text);
  135.     messageParent.appendChild(descriptionNode);
  136.   }
  137.  
  138.   setElementText("info.header", gCommonDialogParam.GetString(3), true);
  139.  
  140.   // set the icon
  141.   var iconElement = document.getElementById("info.icon");
  142.   var iconClass = gCommonDialogParam.GetString(2);
  143.   if (!iconClass)
  144.     iconClass = "message-icon";
  145.   iconElement.setAttribute("class", iconElement.getAttribute("class") + " " + iconClass);
  146.  
  147.   switch (nButtons) {
  148.     case 4:
  149.       setLabelForNode(document.documentElement.getButton("extra2"), gCommonDialogParam.GetString(11));
  150.       // fall through
  151.     case 3:
  152.       setLabelForNode(document.documentElement.getButton("extra1"), gCommonDialogParam.GetString(10));
  153.       // fall through
  154.     default:
  155.     case 2:
  156.       var string = gCommonDialogParam.GetString(9);
  157.       if (string)
  158.         setLabelForNode(document.documentElement.getButton("cancel"), string);
  159.       // fall through
  160.     case 1:
  161.       string = gCommonDialogParam.GetString(8);
  162.       if (string)
  163.         setLabelForNode(document.documentElement.getButton("accept"), string);
  164.       break;
  165.   }
  166.  
  167.   // set default result to cancelled
  168.   gCommonDialogParam.SetInt(0, 1); 
  169.  
  170.   // initialize the checkbox
  171.   setCheckbox(gCommonDialogParam.GetString(1), gCommonDialogParam.GetInt(1));
  172.  
  173.   if (gCommonDialogParam.GetInt(3) == 0) // If no text fields
  174.   {
  175.     var dlgButtons = ['accept', 'cancel', 'extra1', 'extra2'];
  176.  
  177.     // Set the default button and focus it
  178.     var dButton = dlgButtons[gCommonDialogParam.GetInt(5)];
  179.     document.documentElement.defaultButton = dButton;
  180.     document.documentElement.getButton(dButton).focus();
  181.   }
  182.  
  183.   if (gCommonDialogParam.GetInt(6) != 0) // delay button enable
  184.   {
  185.     var delayInterval = 2000;
  186.     try {
  187.       var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  188.                   .getService(Components.interfaces.nsIPrefBranch);
  189.       delayInterval = prefs.getIntPref("security.dialog_enable_delay");
  190.     } catch (e) {}
  191.  
  192.     document.documentElement.getButton("accept").disabled = true;
  193.     document.documentElement.getButton("extra1").disabled = true;
  194.     document.documentElement.getButton("extra2").disabled = true;
  195.  
  196.     setTimeout(commonDialogReenableButtons, delayInterval);
  197.  
  198.     addEventListener("blur", commonDialogBlur, false);
  199.     addEventListener("focus", commonDialogFocus, false);
  200.   }
  201.  
  202.   getAttention();
  203. }
  204.  
  205. var gDelayExpired = false;
  206. var gBlurred = false;
  207.  
  208. function commonDialogBlur(aEvent)
  209. {
  210.   if (aEvent.target != document)
  211.     return;
  212.  
  213.   gBlurred = true;
  214.   document.documentElement.getButton("accept").disabled = true;
  215.   document.documentElement.getButton("extra1").disabled = true;
  216.   document.documentElement.getButton("extra2").disabled = true;
  217. }
  218.  
  219. function commonDialogFocus(aEvent)
  220. {
  221.   if (aEvent.target != document)
  222.     return;
  223.  
  224.   gBlurred = false;
  225.   // When refocusing the window, don't enable the buttons unless the countdown
  226.   // delay has expired.
  227.   if (gDelayExpired) {
  228.     var script = "document.documentElement.getButton('accept').disabled = false; ";
  229.     script += "document.documentElement.getButton('extra1').disabled = false; ";
  230.     script += "document.documentElement.getButton('extra2').disabled = false;";
  231.     setTimeout(script, 250);
  232.   }
  233. }
  234.  
  235. function commonDialogReenableButtons()
  236. {
  237.   // Don't automatically enable the buttons if we're not in the foreground
  238.   if (!gBlurred) {
  239.     document.documentElement.getButton("accept").disabled = false;
  240.     document.documentElement.getButton("extra1").disabled = false;
  241.     document.documentElement.getButton("extra2").disabled = false;
  242.   }
  243.   gDelayExpired = true;
  244. }
  245.  
  246. function initTextbox(aName, aLabelIndex, aValueIndex, aAlwaysLabel)
  247. {
  248.   unHideElementById(aName+"Container");
  249.  
  250.   var label = aLabelIndex < 0 ? "" : gCommonDialogParam.GetString(aLabelIndex);
  251.   if (label || aAlwaysLabel && !label)
  252.     setElementText(aName+"Label", label);
  253.     
  254.   var value = aValueIndex < 0 ? "" : gCommonDialogParam.GetString(aValueIndex);
  255.   var textbox = document.getElementById(aName + "Textbox");
  256.   textbox.setAttribute("value", value);
  257. }
  258.  
  259. function setElementText(aElementID, aValue, aChildNodeFlag)
  260. {
  261.   var element = document.getElementById(aElementID);
  262.   if (!aChildNodeFlag && element) {
  263.     setLabelForNode(element, aValue, true);
  264.   } else if (aChildNodeFlag && element) {
  265.     element.appendChild(document.createTextNode(aValue));
  266.   }
  267. }
  268.  
  269. function setCheckbox(aChkMsg, aChkValue)
  270. {
  271.   if (aChkMsg) {
  272.     // XXX Would love to use hidden instead of collapsed, but the checkbox
  273.     // fails to size itself properly when I do this.
  274.     document.getElementById("checkboxContainer").removeAttribute("collapsed");
  275.     
  276.     var checkboxElement = document.getElementById("checkbox");
  277.     setLabelForNode(checkboxElement, aChkMsg);
  278.     checkboxElement.checked = aChkValue > 0;
  279.   }
  280. }
  281.  
  282. function unHideElementById(aElementID)
  283. {
  284.   var element = document.getElementById(aElementID);
  285.   element.hidden = false;
  286. }
  287.  
  288. function hideElementById(aElementID)
  289. {
  290.   var element = document.getElementById(aElementID)
  291.   element.hidden = true;
  292. }
  293.  
  294. function isVisible(aElementId)
  295. {
  296.   return document.getElementById(aElementId).hasAttribute("hidden");
  297. }
  298.  
  299. function onCheckboxClick(aCheckboxElement)
  300. {
  301.   gCommonDialogParam.SetInt(1, aCheckboxElement.checked);
  302. }
  303.  
  304. function commonDialogOnAccept()
  305. {
  306.   gCommonDialogParam.SetInt(0, 0); // say that ok was pressed
  307.  
  308.   var numTextBoxes = gCommonDialogParam.GetInt(3);
  309.   var textboxIsPassword1 = gCommonDialogParam.GetInt(4) == 1;
  310.   
  311.   if (numTextBoxes >= 1) {
  312.     var editField1;
  313.     if (textboxIsPassword1)
  314.       editField1 = document.getElementById("password1Textbox");
  315.     else
  316.       editField1 = document.getElementById("loginTextbox");
  317.     gCommonDialogParam.SetString(6, editField1.value);
  318.   }
  319.  
  320.   if (numTextBoxes == 2) {
  321.     var editField2;
  322.     if (textboxIsPassword1)
  323.       // we had two password fields
  324.       editField2 = document.getElementById("password2Textbox");
  325.     else
  326.       // we only had one password field (and one login field)
  327.       editField2 = document.getElementById("password1Textbox");
  328.     gCommonDialogParam.SetString(7, editField2.value);
  329.   }
  330. }
  331.  
  332. function commonDialogOnExtra1()
  333. {
  334.   gCommonDialogParam.SetInt(0, 2);
  335.   window.close();
  336. }
  337.  
  338. function commonDialogOnExtra2()
  339. {
  340.   gCommonDialogParam.SetInt(0, 3);
  341.   window.close();
  342. }
  343.